<%--
  Created by IntelliJ IDEA.
  User: 大松
  Date: 2022/10/24
  Time: 20:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改密码</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui-v2.7.6/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/static/layui-v2.7.6/layui/layui.js"></script>
</head>
<body>
<form class="layui-form" action=""  id="saveUserForm" lay-filter="updateUserFormFilter">
    <input type="hidden" name="id" value="${user.id}" id="userid">
    <input type="hidden" name="username" value="${user.username}" id="usename">

    <form class="layui-form" enctype="multipart/form-data" action="" >

        <div class="layui-form-item pass" >
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password"  lay-verify="required" onblur="checkPassword(this)" placeholder="请输入旧密码" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item pass" >
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password"  lay-verify="required" placeholder="请输入新密码"
                       autocomplete="off" class="layui-input" id="pass1">
            </div>
        </div>

        <div class="layui-form-item pass" >
            <label class="layui-form-label">确认新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password"  lay-verify="required|pass" placeholder="请再次输入新密码"
                       autocomplete="off" class="layui-input" id="pass2">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSaveUser">修改</button>
                <button class="layui-btn" type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button class="layui-btn" onclick="exit()">返回</button>
            </div>
        </div>
    </form>
</form>
<script src="${pageContext.request.contextPath}/static/jquery-3.6.1.min.js"></script>
<script>
    var passwordStatus=true;
    $(function () {
        layui.use(['layer','form','jquery'], function(){
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.jquery;
            var namestatus=true;

            // 自定义校验  验证两次密码是不是一样
            form.verify({
                pass: function(value){ //自定义lay-verify的昵称，value：表单的值
                    var pass1 = $("#pass1").val();
                    if(value!=pass1){
                        return "两次密码不一致！！！";
                    }

                }
            });
            //提交表单
            // Layui提交表单 监听提交  data代表的是form表单里面的所有的内容
            form.on('submit(formSaveUser)', function(data){
                var id=$("#userid").val();
                var password=$("#pass1").val();
                var params = {};
                params.id=id;
                params.password=password;
                namestatus=passwordStatus;
                if(namestatus){
                    $.ajax({
                        url:"${pageContext.request.contextPath}/user/updPassword",
                        type:"post",
                        dataType:"JSON",
                        data:params,//表单里所有数据
                        success:function (ret){
                            if (ret.data == true){
                                layer.msg("修改成功",{icon:"1"});
                                <%--/*window.location="${pageContext.request.contextPath}/view/login.jsp"*/--%>
                            }
                        }
                    })
                }
                return false;
            })
        })
    })
    function checkPassword(obj){
        var username = $("#usename").val();
        var password=obj.value;
        var params = {};
        params.username=username;
        params.password=password;
        $.ajax({
            url:"${pageContext.request.contextPath}/login",
            type:"post",
            dataType:"JSON",
            data:params,//表单里所有数据
            success:function (ret){
                if (ret.data == true){
                    layer.msg("密码正确",{icon:"1"});
                    passwordStatus=true;
                    //关闭所有弹框
                }else{
                    layer.msg("旧密码错误",{icon:"2"});
                    passwordStatus=false;
                    //关闭所有弹框
                }
            }
        })
    }
    // //修改密码重新登录
    // function exit(){
    //     if(confirm("请重新登录")){
    //
    //     }
    // }
</script>
</body>
</html>
